package com.prodavalnik.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.prodavalnik.model.DataBaseController;
import com.prodavalnik.model.User;

/**
 * Servlet implementation class ChangeProfileServlet
 */
@WebServlet("/ChangeProfileServlet")
public class ChangeProfileServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ChangeProfileServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		PrintWriter writer = response.getWriter();

		HttpSession session = request.getSession(false);
		
		if(session==null){
			writer.write("error");
			return;
		}
	
		String user = (String) session.getAttribute("username");
		String address = request.getParameter("address");
		String phone = request.getParameter("phone");
		String password = request.getParameter("password");
		String repeatedPassword = request.getParameter("repeatPass");
				
		User userObject = null;
		try {
			userObject = DataBaseController.returnUserByUsernameAndPassword(user,password);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
	
	
			
		if(!password.equals(repeatedPassword) || userObject==null){
			writer.write("Password isn't correct or Passwords don't match");
			return;
		}
		
		else if(address==null && phone==null){
			try {
				DataBaseController.changeUser(user,password, userObject.getEmail(), userObject.getConn());
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		else if(address==null && phone!=null){
			try {
				DataBaseController.changeUser(user, password, userObject.getEmail(), userObject.getConn().endsWith(",")+","+phone);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		else if(phone==null && address!=null){
			try {
				DataBaseController.changeUser(user,password, userObject.getEmail(), address+","+userObject.getConn().startsWith(","));
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		else{
			try {
				DataBaseController.changeUser(user,password, userObject.getEmail(), address+","+phone);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		writer.write("Successfully updated profile!");

		
	}

}
